<?php
    class PaymentTotalController extends CpController
    {
        public function actionIndex()
        {   
            $data_sms_month=array();
            $data_card_month=array();
            $data_charging_month=array();
            $data_sms_day=array();
            $data_card_day=array();
            $data_charging_day=array();
            $total_sms=0;
            $total_card=0;
            $total_charging=0;
            $chart_type=isset($_GET['chart_type'])? intval($_GET['chart_type']):1;
            $month=isset($_GET['month'])? intval($_GET['month']):intval(date('m'));
            $year=isset($_GET['year'])? intval($_GET['year']):2013;
            $is_sms=isset($_GET['is_sms'])? intval($_GET['is_sms']):1;
            $is_card=isset($_GET['is_card'])? intval($_GET['is_card']):2;
            $is_charging=isset($_GET['is_charging'])? intval($_GET['is_charging']):3;
            $rate = APaymentRevenue::getAllRate();
            $rate_sms = APaymentRevenue::getAllRateSms();
            $rate_card=APaymentRevenue::getAllRateCard();
            if($chart_type==1){

                $count = cal_days_in_month(CAL_GREGORIAN, $month, $year);
                $dataSms=APaymentTotal::getDataSmsByMonthAndYear($month,$year);
                $dataCard=APaymentTotal::getDataCardByMonthAndYear($month,$year);
                $dataCharging=APaymentTotal::getDataChargingByMonthAndYear($month,$year);
                for($i=1;$i<=$count;$i++){
                    $money_sms_day[$i]=0;
                    $money_card_day[$i]=0;
                    $money_charging_day[$i]=0;
                    foreach($dataSms as $key=>$value){
                        $index_rate = $value["month"] . "_" . $value["year"];
                        if($value['day']==$i){
                            $sc = preg_replace("/(\d+)(\d+)(\d+)(\d+)/si", "X$2XX", $value["serviceNumber"]);
                            $rateSms=$rate_sms[$index_rate][$sc]/100;
                            $money_telco=round($value['count']*$value['price']*$rateSms);
                            $money_sms_day[$i]=$money_sms_day[$i]+$money_telco;
                        }
                    }
                    foreach($dataCard as $key=>$value){
                        if($value['day']==$i){
                            $rateCard=$rate_card[$value['month']][$value['year']]/100;
                            $money_telco=round($value['count']*$value['price']*$rateCard);
                            $money_card_day[$i]=$money_card_day[$i]+$money_telco;
                        }
                    }
                    foreach($dataCharging as $key=>$value){
                        if($value['day']==$i){
                            $money_telco=round($value['count']*$value['price_telco']);
                            $money_charging_day[$i]=$money_charging_day[$i]+$money_telco;
                        }
                    }
                    $total_sms=$total_sms+$money_sms_day[$i];
                    $total_card=$total_card+$money_card_day[$i];
                    $total_charging=$total_charging+$money_charging_day[$i];
                }
                $this->render(
                "index",
                array(
                "chart_type"=>$chart_type, 
                "month"=>$month,
                "year"=>$year,
                "chart_type"=>$chart_type,
                "is_sms"=>$is_sms,
                "is_card"=>$is_card,
                "is_charging"=>$is_charging,
                "money_sms_day"=>$money_sms_day,
                "money_card_day"=>$money_card_day,
                "money_charging_day"=>$money_charging_day,
                "total_sms"=>$total_sms,
                "total_card"=>$total_card,
                "total_charging"=>$total_charging,
                )
                );    
            }
            if($chart_type==2){
                $dataSms=APaymentTotal::getDataSmsByYear($year);
                $dataCard=APaymentTotal::getDataCardByYear($year);
                $dataCharging=APaymentTotal::getDataChargingByYear($year);
                for($i=1;$i<=12;$i++){
                    $money_sms_month[$i]=0;
                    $money_card_month[$i]=0;
                    $money_charging_month[$i]=0;
                    foreach($dataSms as $key=>$value){
                        $index_rate = $value["month"] . "_" . $value["year"];
                        if($value['month']==$i){
                            $sc = preg_replace("/(\d+)(\d+)(\d+)(\d+)/si", "X$2XX", $value["serviceNumber"]);
                            $rateSms=$rate_sms[$index_rate][$sc]/100;
                            $money_telco=round($value['count']*$value['price']*$rateSms);
                            $money_sms_month[$i]=$money_sms_month[$i]+$money_telco;
                        }
                    }
                    foreach($dataCard as $key=>$value){
                        if($value['month']==$i){
                            $money_telco=round($value['count']*$value['price']*0.85);
                            $money_card_month[$i]=$money_card_month[$i]+$money_telco;
                        }
                    }
                    foreach($dataCharging as $key=>$value){
                        if($value['month']==$i){
                            $money_telco=round($value['count']*$value['price_telco']);
                            $money_charging_month[$i]=$money_charging_month[$i]+$money_telco;
                        }
                    }
                    $total_sms=$total_sms+$money_sms_month[$i];
                    $total_card=$total_card+$money_card_month[$i];
                    $total_charging=$total_charging+$money_charging_month[$i];
                }

                $this->render(
                "index",
                array(
                "chart_type"=>$chart_type, 
                "month"=>$month,
                "year"=>$year,
                "chart_type"=>$chart_type,
                "is_sms"=>$is_sms,
                "is_card"=>$is_card,
                "is_charging"=>$is_charging,
                "money_sms_month"=>$money_sms_month,
                "money_card_month"=>$money_card_month,
                "money_charging_month"=>$money_charging_month,
                "total_sms"=>$total_sms,
                "total_card"=>$total_card,
                "total_charging"=>$total_charging,
                )
                );    
            }
        }
    }  
?>
